package com.sumapay.fsb.gensrc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sumapay.fsb.common.util.CommonPage;
import com.sumapay.fsb.gensrc.entity.TradeRecord;
import com.sumapay.fsb.gensrc.mapper.TradeRecordMapper;
import com.sumapay.fsb.gensrc.service.ITradeRecordService;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;

import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author suma
 * @since 2021-02-05
 */
@Service
public class TradeRecordServiceImpl extends ServiceImpl<TradeRecordMapper, TradeRecord> implements ITradeRecordService {


    @Override
    public boolean updateOrgTradeRecord(String result, TradeRecord tt) {
        JSONObject jr = JSONObject.parseObject(result);
        tt.setStatus(jr.getString("ACTION_RESULT"));
        tt.setRespCode(jr.getString("RESP_CODE"));
        tt.setRespMsg(jr.getString("RESP_MSG"));
        tt.setVoucherNum(jr.getString("VOUCHER_NUM"));

        UpdateWrapper<TradeRecord> wrapper = new UpdateWrapper<>();
        wrapper.eq("TXN_NO", tt.getTxnNo()).eq("TRADE_ID", tt.getTradeId());
        return update(tt, wrapper);
    }


    @Override
    public CommonPage<TradeRecord> queryList(Map<String, Object> map) {

        long current = Long.parseLong(map.get("CURRENT_PAGE") != null ? (String) map.get("CURRENT_PAGE") : "1");
        long size = Long.parseLong(map.get("PAGE_SIZE") != null ? (String) map.get("PAGE_SIZE") : "500");
        IPage<TradeRecord> userPage = new Page<>(current, size);

        QueryWrapper<TradeRecord> queryWrapper = new QueryWrapper();
        //queryWrapper.eq("BANK_ACCOUNT", map.get("BANK_ACCOUNT"));
        queryWrapper.eq("PHONE_NO", map.get("PHONE_NO"));
        //queryWrapper.eq("BANK_ACCOUNT_NAME", map.get("BANK_ACCOUNT_NAME"));
        //queryWrapper.eq("ID_NUMBER", map.get("ID_NUMBER"));
        if (map.get("TRADE_START_TIME") != null) {
            queryWrapper.gt("TRADE_START_TIME", map.get("TRADE_START_TIME"));
        }
        if (map.get("TRADE_END_TIME") != null) {
            queryWrapper.lt("TRADE_END_TIME", map.get("TRADE_END_TIME"));
        }
        String ss = queryWrapper.getExpression().getSqlSegment();
        System.out.println(ss);
        queryWrapper.orderBy(true, false, "ROW_CREATE_TIME");

        IPage<TradeRecord> lp = baseMapper.selectPage(userPage, queryWrapper);

        CommonPage<TradeRecord> cp = new CommonPage<>();
        cp.setRecords(lp.getRecords());
        cp.setCurrent(lp.getCurrent() + "");
        cp.setPages(lp.getPages()+ "");
        cp.setSize(lp.getSize()+ "");
        cp.setTotal(lp.getTotal()+ "");
        cp.setACTION_RESULT(0);
        return cp;

    }

}
